<template>
  <div>
    <Card>
      <!-- 商品分类 -->
      <!-- 标题 -->
      <div class="title" slot="title">
        <span>商品分类</span>
        <el-button @click="addType" size="medium" type="primary"
          >添加分类</el-button
        >
      </div>
      <!-- 内容 -->
      <div slot="content">
        <el-table :data="tableData" style="width: 100%">
          <!-- 序号 -->
          <el-table-column label="序号" width="120" type="index">
          </el-table-column>

          <!-- 分类名称 -->
          <el-table-column prop="cateName" label="分类名称" width="380">
            <template slot-scope="scope">
              <span v-if="!scope.row.flag">{{ scope.row.cateName }}</span>
              <el-input
                v-else
                v-model="scope.row.cateName"
                size="mini"
              ></el-input>
            </template>
          </el-table-column>

          <!-- 是否启用 -->
          <el-table-column prop="state" label="是否启用" show-overflow-tooltip>
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.state"
                :disabled="!scope.row.flag"
                active-color="#13ce66"
              >
              </el-switch>
            </template>
          </el-table-column>
          <!-- 按钮 -->
          <el-table-column label="操作" show-overflow-tooltip>
            <template slot-scope="scope">
              <el-button
                v-if="!scope.row.flag"
                size="mini"
                @click="handleEdit(scope.row)"
                >编辑</el-button
              >
              <el-button
                v-else
                size="mini"
                type="success"
                @click="handleOk(scope.row)"
                >完成</el-button
              >
              <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
        <!-- 模态框 -->
        <el-dialog title="添加分类" :visible.sync="dialogVisible" width="600px">
          <el-form
            ref="ruleForm"
            :rules="rules"
            :model="ruleForm"
            label-width="80px"
            class="ruleForm"
          >
            <el-form-item label="分类名称" prop="cateCateName">
              <el-input
                v-model="ruleForm.cateName"
                placeholder="分类名称"
              ></el-input>
            </el-form-item>

            <el-form-item label="是否禁用">
              <el-switch
                v-model="ruleForm.state"
                active-color="#13ce66"
                inactive-color="#ff4949"
              >
              </el-switch>
            </el-form-item>
          </el-form>

          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="dialogOk">确 定</el-button>
          </span>
        </el-dialog>
      </div>
    </Card>
  </div>
</template>

<script>
import Card from "@/components/Card.vue";
import { GOODS_REG } from "@/utilis/reg.js";
import { addType, getType, eiditGoods, delType } from "@/apis/goods.js";
export default {
  components: {
    Card,
  },

  data() {
    const goodsReg = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入商品名称"));
      } else if (!GOODS_REG.test(value)) {
        callback(new Error("请输入2~9位字符"));
      } else {
        callback();
      }
    };
    return {
      // 模态框显示隐藏
      dialogVisible: false,
      // 表格数据
      tableData: [],
      rules: {
        cateName: {
          validator: goodsReg,
          trigger: "blur",
        },
      },
      // 编辑功能表单数据
      ruleForm: {
        cateName: "",
        state: true,
      },
      currentPage: 1, // 当前页数
      total: 0, //总页数
      pageSize: 5, //每页显示的条数
    };
  },
  // 生命周期
  created() {
    this.typeList();
  },
  methods: {
    // 获取分类列表
    async typeList() {
      let { data, total } = await getType({
        currentPage: this.currentPage,
        pageSize: this.pageSize,
      });
      data.forEach((item) => {
        (item.state = item.state === 1 ? true : false), (item.flag = false);
      });
      this.total = total;
      this.tableData = data;
      if (!data.length && this.currentPage != 1) {
        this.currentPage -= 1;
        // 更新视图
        this.typeList();
      }
    },

    // 每条页数改变时会触发
    handleSizeChange(val) {
      this.pageSize = val;
      this.typeList();
    },
    // 当前页改变时触发
    handleCurrentChange(val) {
      this.currentPage = val;
      this.typeList();
    },
    // 单条操作按钮
    handleEdit(row) {
      row.flag = !row.flag;
    },
    // 单条完成按钮
    async handleOk(row) {
      let res = await eiditGoods({
        id: row.id,
        cateName: row.cateName,
        state: row.state,
      });
      row.flag = !row.flag;
    },
    // 单条删除按钮
    handleDelete(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          await delType({ id: row.id });
          this.typeList();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 模态框确定按钮
    dialogOk() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          let { code } = await addType(this.ruleForm);
          if (code == 0) {
            this.ruleForm.cateName = "";
            this.ruleForm.state = true;
            this.dialogVisible = false;
            this.typeList();
          }
        } else {
          console.log("验证不通过");
        }
      });
    },
    // 添加分类
    addType() {
      this.dialogVisible = true;
    },
  },
};
</script>

<style lang="less" scoped>
.title {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.el-pagination {
  margin-top: 10px;
}
</style>